logo

The best IT Trainig Institute In Gurgaon

Soft Assertion in TestNG

package asc;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import io.github.bonigarcia.wdm.WebDriverManager;

public class VerifyTitleAndText {

	@Test
	public void titleTest() {
		
		SoftAssert softAssert = new SoftAssert();
		
		
		String expectedTitle = "Electronics, Cars, Fashion, Collectibles & More | eBay";
		String expectedText = "Search";
		WebDriverManager.chromedriver().setup();
		ChromeDriver driver = new ChromeDriver();
		driver.get("https://www.ebay.com/");
		
		String actualtitle = driver.getTitle();
		System.out.println(actualtitle);
		System.out.println("Verifying Title");
		softAssert.assertEquals(actualtitle, expectedTitle, "Title Verification Failed");
		
		String actualText = driver.findElement(By.xpath("//*[@id=\"gh-btn\"]")).getAttribute("value");
		System.out.println(actualText);
		System.out.println("Verifying text");
		softAssert.assertEquals(actualText, expectedText, "Text Varification Failed");
		System.out.println("Closing Browser");
		
		driver.close();
		softAssert.assertAll();
		
		
	}
}
                
Package and Imports
package asc;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import io.github.bonigarcia.wdm.WebDriverManager;
                    
  • package asc;: Declares the package name asc for this class.
  • import org.openqa.selenium.By;:
    Imports the By class from Selenium, used to locate elements on the webpage.
  • import org.openqa.selenium.chrome.ChromeDriver;:
    Imports the ChromeDriver class from Selenium.
  • import org.testng.annotations.Test;:
    Imports the Test annotation from TestNG.
  • import org.testng.asserts.SoftAssert;:
    Imports the SoftAssert class from TestNG, which allows multiple assertions in a single test.
Test Class and Method
public class VerifyTitleAndText {

    @Test
    public void titleTest() {
        SoftAssert softAssert = new SoftAssert();
        
        String expectedTitle = "Electronics, Cars, Fashion, Collectibles & More | eBay";
        String expectedText = "Search";
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.ebay.com/");
        
        String actualtitle = driver.getTitle();
        System.out.println(actualtitle);
        System.out.println("Verifying Title");
        softAssert.assertEquals(actualtitle, expectedTitle, "Title Verification Failed");
        
        String actualText = driver.findElement(By.xpath("//*[@id=\"gh-btn\"]")).getAttribute("value");
        System.out.println(actualText);
        System.out.println("Verifying text");
        softAssert.assertEquals(actualText, expectedText, "Text Verification Failed");
        System.out.println("Closing Browser");
        
        driver.close();
        softAssert.assertAll();
    }
}
                    
  1. Soft Assertion Setup:
    • SoftAssert softAssert = new SoftAssert();:
      A Soft Assert object is created, which allows the test to continue executing even if an assertion fails. The results of all assertions are collected and reported together at the end of the test.
  2. Expected Values:
    • String expectedTitle = "Electronics, Cars, Fashion, Collectibles & More | eBay";:
      This variable holds the expected title of the eBay homepage. (Note: This title intentionally contains a typo to demonstrate a failing assertion.)
    • String expectedText = "Search";:
      This variable holds the expected text for a specific element on the page.
  3. Setting Up WebDriver:
    • WebDriverManager.chromedriver().setup();: 
      Automatically configures the ChromeDriver.
    • ChromeDriver driver = new ChromeDriver();: 
      Creates a new instance of ChromeDriver to control the Chrome browser.
  4. Navigating to URL:
    • driver.get("https://www.ebay.com/");: 
      Opens the eBay homepage.
  5. Retrieving and Verifying the Page Title:
    • String actualtitle = driver.getTitle();: 
      Retrieves the title of the current webpage.
    • System.out.println(actualtitle);: 
      Prints the actual title to the console.
    • softAssert.assertEquals(actualtitle, expectedTitle, "Title Verification Failed");: 
      Soft assertion to check if the actual title matches the expected title. If it fails, the test will continue, and the error message "Title Verification Failed" will be reported later.
  6. Retrieving and Verifying Text of an Element:
    • String actualText = driver.findElement(By.xpath("//*[@id=\"gh-btn\"]")).getAttribute("value");: 
      Finds the element by its XPath and retrieves its value attribute.
    • System.out.println(actualText);: 
      Prints the actual text of the element to the console.
    • softAssert.assertEquals(actualText, expectedText, "Text Verification Failed");: 
      Soft assertion to check if the actual text matches the expected text. If it fails, the test will still continue.
  7. Closing the Browser:
    • driver.close();: 
      Closes the browser window after the test.
  8. Final Assertion Check:
    • softAssert.assertAll();: 
      This method is crucial in soft assertions. It checks all the soft assertions that were made during the test. If any of them failed, assertAll() will throw an exception, causing the test to fail and report all failed assertions.